using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysFrame.Message;
using System.Reflection;

namespace SysFrame.BP
{
    /// <summary>
    /// 字典system_4方法
    /// </summary>
    public class BLLsystem_4
    {
        public Isystem_4 interface_system_4;

        public BLLsystem_4()
        {
            object obj = BPBase.GetAchieve("SysFrame.BP", "system_4");
            interface_system_4 = (Isystem_4)obj;
        }


        /// <summary>
        /// 得到system_4实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Mssystem_4> Getsystem_4MsByQuery(MsQuery msq)
        {
            return interface_system_4.Getsystem_4MsByQuery(msq);
        }

        /// <summary>
        /// 通过System_4_10得到实体
        /// </summary>
        /// <param name="System_4_10">ID</param>
        /// <returns>实体</returns>
        public Mssystem_4 Getsystem_4MsByKey(string System_4_10)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and System_4_10='" + System_4_10 + "' ";
            List<Mssystem_4> lstData = Getsystem_4MsByQuery(msquery);
            return lstData[0];
        }

        /// <summary>
        ///  保存system_4信息
        /// </summary>
        /// <param name="ms">实体信息</param>
        /// <returns>消息</returns>
        public MsReturned Savesystem_4(Mssystem_4 ms)
        {
            return interface_system_4.Savesystem_4(ms);
        }

        /// <summary>
        ///  保存system_4信息 根据name判断修改还是新增
        /// </summary>
        /// <param name="type">资产类型</param>
        /// <param name="name">名称</param>
        /// <param name="value1">值1</param>
        /// <param name="value2">值2</param>
        /// <returns>消息</returns>
        public MsReturned Savesystem_4(string type, string name, string value1, string value2)
        {
            Mssystem_4 ms = null;
            //通过name得到ms如果没有就新增
            MsQuery msq = new MsQuery();
            msq.strquery += " and SYSTEM_4_30 ='" + type + "'";
            msq.strquery += " and SYSTEM_4_40 ='" + name + "'";
            try
            {
                ms = Getsystem_4MsByQuery(msq)[0];
            }
            catch { }
            if (ms == null || ms.System_4_10 == 0)
            {
                ms = new Mssystem_4();
                ms.System_4_20 = GetMaxByTypeCode(type);
            }
            ms.System_4_30 = type;
            ms.System_4_40 = name;
            ms.System_4_50 = value1;
            ms.System_4_60 = value2;
            ms.System_4_70 = "1";
            return interface_system_4.Savesystem_4(ms);
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Getsystem_4ByQuery(MsQuery msquery)
        {
            return interface_system_4.Getsystem_4ByQuery(msquery);
        }

        /// <summary>
        /// 删除system_4
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_4ByQuery(MsQuery msq)
        {
            return interface_system_4.Deletesystem_4ByQuery(msq);
        }
        /// <summary>
        /// 删除system_4
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_4ByKey(string System_4_10)
        {
            MsQuery msquery = new MsQuery();

            if (System_4_10.IndexOf(",") > 0)
            {
                msquery.strquery += " and System_4_10 in (" + System_4_10 + ") ";
            }
            else
            {
                msquery.strquery += " and System_4_10='" + System_4_10 + "' ";
            }
            return interface_system_4.Deletesystem_4ByQuery(msquery);
        }

        /// <summary>
        /// 通过字典类型编号得到字典集合
        /// </summary>
        /// <param name="typecode">字典分类编号</param>
        /// <returns></returns>
        public List<Mssystem_4> Getsystem_4ByTypeCode(string typecode)
        {
            List<Mssystem_4> listms = new List<Mssystem_4>();
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and SYSTEM_4_30 in ('" + typecode + "') ";
            return interface_system_4.Getsystem_4MsByQuery(msquery);
        }

        /// <summary>
        /// 通过字典编号得到字典
        /// </summary>
        /// <param name="typecode">字典分类编号</param>
        /// <returns></returns>
        public Mssystem_4 Getsystem_4ByCode(string code)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and SYSTEM_4_20 in ('" + code + "') ";
            List<Mssystem_4> listms = interface_system_4.Getsystem_4MsByQuery(msquery);
            if (listms.Count == 1)
            {
                return listms[0];
            }
            else { return new Mssystem_4(); }
        }
        /// <summary>
        /// 得到最新编号
        /// </summary>
        /// <param name="typecode">分类ID</param>
        /// <returns></returns>
        public string GetMaxByTypeCode(string typecode)
        {
            int n = 1;
            List<Mssystem_4> listms = Getsystem_4ByTypeCode(typecode);
            if (listms != null)
            {
                n += listms.Count;
            }
            return typecode + n.ToString().PadLeft(3, '0');
        }
        
    }
}
